package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.entity.DepartmentEntity;
import com.example.demo.entity.MinorityEntity;
import com.example.demo.entity.UserEntity;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.UserService;
import com.example.demo.tool.ResponseResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@Api(tags = "登录注册相关接口")
public class login {
    @Autowired
    private UserService userService;

    @PostMapping("registered")
    @ApiOperation(value = "注册用户",notes = "用户注册,验证两次密码")
    public ResponseResult registered(UserEntity userEntity){
//        UserEntity entity = userService.getOne(new QueryWrapper<UserEntity>().eq("email", userEntity.getEmail()));
        UserEntity entity = userService.getOne(new QueryWrapper<UserEntity>().eq("email", userEntity.getEmail()));
        if (entity==null){
            userEntity.setCreateTime(new Date());
            userService.save(userEntity);
            return ResponseResult.success();
        }
        if (userEntity.getEmail().equals(entity.getEmail())){
            return ResponseResult.error();
        }
        return ResponseResult.success();
    }

    @GetMapping("myuser/login")
    @ApiOperation("登录")
    public ResponseResult login(String email, String password){
        UserEntity userEntity = userService.getOne(new QueryWrapper<UserEntity>().eq("email", email));
        if (userEntity==null){
            return ResponseResult.error("false");
        }
        if (userEntity.getPassword().equals(password)){
            return ResponseResult.success("true");
        }
        return ResponseResult.error("false");
    }

    @GetMapping("getInfo")
    @ApiOperation("获取用户信息")
    public ResponseResult getInfo(String email){
        return ResponseResult.success(userService.getOne(new QueryWrapper<UserEntity>().eq("email", email)));
    }

    @GetMapping("getAllusers")
    @ApiOperation("获取全部用户信息")
    public ResponseResult getUsers(@ApiParam("页码，默认为1") @RequestParam(name = "pageNum",defaultValue = "1") Integer pageNum,
                                   @ApiParam("每页数量，默认为10") @RequestParam(name = "pageSize",defaultValue = "10") Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<UserEntity> list = userService.list(new QueryWrapper<UserEntity>().orderByDesc("create_time"));
        PageInfo<UserEntity> page = new PageInfo<>(list);
        return ResponseResult.success(page);
    }

    @GetMapping("deleteUser")
    @ApiOperation("删除用户")
    public ResponseResult deletelist(Integer id){
        userService.removeById(id);
        return ResponseResult.success();
    }

    @PostMapping("editUser")
    @ApiOperation("根据id修改一条用户信息")
    public ResponseResult updateuser(Integer id,UserEntity userEntity){
        userService.updateById(userEntity);
        return ResponseResult.success();
    }
}
